<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{	
	$billMasterArr = 0;
  $masterArray = array();
  $detailArray = array();
  $billDetailId = "";
 
  
  if(isset($_POST['updateBtn']) && $_POST['billDetailId'] > 0)
  {
  	$date             = $_POST['outDateYear']."-".$_POST['outDateMonth']."-".$_POST['outDateDay'];
  	$outChallenNo     = (isset($_POST['outChallenNo'])) && ($_POST['outChallenNo'] != "") ? $_POST['outChallenNo'] : 0 ;
  	$finalProcess     = (isset($_POST['finalProcess'])) && ($_POST['finalProcess'] != "") ? $_POST['finalProcess'] : 0 ;
  	$outQuantity      = (isset($_POST['outQuantity'])) && ($_POST['outQuantity'] != "") ? $_POST['outQuantity'] : 0 ;
  	$outWieght        = (isset($_POST['outWieght'])) && ($_POST['outWieght'] != "") ? $_POST['outWieght'] : 0 ;
  	$outRate          = (isset($_POST['outRate'])) && ($_POST['outRate'] != "") ? $_POST['outRate'] : 0 ;
  	$amount           = (isset($_POST['amount'])) && ($_POST['amount'] != "") ? $_POST['amount'] : 0 ;
  
    $updateDetail = "UPDATE billdetail
                        SET finalProcess = '".$finalProcess."',
                            outQuantity  = '".$outQuantity."',
                            outWieght    = '".$outWieght."',
                            outRate      = '".$outRate."',
                            amount       = '".$amount."'
                      WHERE billDetailId = ".$_REQUEST['billDetailId'];                         			  
    $updateDetailResult = mysql_query($updateDetail); 
    if(!$updateDetailResult)
    {
      die("Update Query Not Updated for Master Table : ".mysql_error());
    }
    else
    {
      header("Location:billList.php");                   
    }
  }
	
	if(isset($_GET['billDetailId']) > 0)
	{ 
	  $billDetailId = $_GET['billDetailId'];
		$dCount =0; 
    $selBillDetail = "SELECT billDetailId,billdetail.*, DATE_FORMAT(outgoingmaster.date,'%d-%m-%y') AS outDate, outChallenNo,
                             incoming.challan AS inChallenNo,materialName,material.materialId
                        FROM billdetail
                        JOIN outgoingmaster ON billdetail.outGoingMasterId = outgoingmaster.outGoingMasterId
                        JOIN incoming ON outgoingmaster.incomingId = incoming.incomingId
                        JOIN material ON incoming.materialId = material.materialId
                       WHERE billDetailId = ".$_GET['billDetailId'];
    $selBillDetailRes = mysql_query($selBillDetail);
    while($dRow = mysql_fetch_array($selBillDetailRes))
    {
      $detailArray[$dCount]['billMasterId'] = $dRow['billMasterId'];
      $detailArray[$dCount]['billDetailId'] = $dRow['billDetailId'];
      $detailArray[$dCount]['outDate']      = $dRow['outDate'];
      $detailArray[$dCount]['outChallenNo'] = $dRow['outChallenNo'];
      $detailArray[$dCount]['inChallenNo']  = $dRow['inChallenNo'];
      $detailArray[$dCount]['materialId']   = $dRow['materialId'];
      $detailArray[$dCount]['materialName'] = $dRow['materialName'];
      $detailArray[$dCount]['finalProcess'] = $dRow['finalProcess'];
      $detailArray[$dCount]['outQuantity']  = $dRow['outQuantity'];
      $detailArray[$dCount]['outWieght']    = $dRow['outWieght'];
      $detailArray[$dCount]['outRate']      = $dRow['outRate'];
      $detailArray[$dCount]['amount']       = $dRow['amount'];
      $dCount++;
    }
	}
	
  $k = 0;
  $partyArray = array();
  $partyId     = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  $selectParty = "SELECT partyId,partyName
                    FROM party
                   WHERE partyId IN (SELECT partyId FROM billmaster)
                   ORDER BY partyName";
  $selectPartyResult = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyResult))
  {
    $partyArray['partyId'][$k]   = $partyRow['partyId'];
    $partyArray['partyName'][$k] = $partyRow['partyName'];
    $k++;
  }
  
  $m = 0;
  $catmaterialArray = array();
  $select = "SELECT materialId,materialName                                    
               FROM material
              ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}

	$r = 0;
	$catprocessArray = array();
	$select = "SELECT finalProcessId,finalProcessName                                    
	             FROM finalprocess";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catprocessArray['finalProcessId'][$r]   = $row['finalProcessId'];                    
	  $catprocessArray['finalProcessName'][$r] = $row['finalProcessName'];                      
	  $r++;                                                                     
	}    
  include("./bottom.php");
  $smarty->assign("billMasterArr",$billMasterArr);
  $smarty->assign("partyId",$partyId);
  $smarty->assign("partyArray",$partyArray);
  $smarty->assign("masterArray",$masterArray);
  $smarty->assign("detailArray",$detailArray);
  $smarty->assign("catmaterialArray",$catmaterialArray);
  $smarty->assign("catprocessArray",$catprocessArray);
  $smarty->assign("billDetailId",$billDetailId);
  $smarty->display("billEditDetail.tpl");
}
?>